regular exp help needed

Hello,

I need a regular expression for the following:

 

string textstr ="ABC, sections 3.3.1.9, 3.3.7.1, 3.3.7.2, 3.3.9.5, 3.3.9.6.1, 3.3.9.6.2, 3.3.9.6.3, 3.3.9.6.4, 3.3.9.6.5, 3.3.12.1, 3.3.12.2, and 3.3.13.1 and DEF, sections 7.7.4.4 and 8.5.5." => "3.3.1.9, 3.3.7.1, 3.3.7.2, 3.3.9.5, 3.3.9.6.1, 3.3.9.6.2, 3.3.9.6.3, 3.3.9.6.4, 3.3.9.6.5, 3.3.12.1, 3.3.12.2, and 3.3.13.1"
//string textstr ="DEF, sections 7.7.4.2 and 8.2.4 and ABC, sections 3.3.9.2 and 3.3.9.3." => "3.3.9.2 and 3.3.9.3"
//string textstr ="ABC, section 3.3.7.2 and DEF, sections 3.6.2.4, 7.5.2.6, 7.5.2.8, and 7.5.2.9." => "3.3.7.2"
//string textstr ="DEF, section 3.6.2 and ABC, section 3.3.1.1." => "3.3.1.1"
//string textstr ="DEF, section 7.7.3.4 and ABC, section 3.3.1.9." => "3.3.1.9"
//string textstr ="ABC, section 4.3.1." => "4.3.1"

Regexp line = regexp2 "ABC, sections*([^and]*)([and]*)([^and]*)"

if (line textstr) {
    print textstr[match 0] "(0)\n" // whole match
}

 


EHcnck - Wed Jan 08 22:32:00 EST 2014

Re: regular exp help needed
M_vdLaan - Thu Jan 09 09:55:01 EST 2014

Hi Ehcnck,

 

I think the following does what you need it to.

 

Regexp line = regexp2 "ABC, (section ([0-9.]*[0-9])|sections ( *[0-9.]*[0-9][ ,]*(and *[0-9.]*[0-9])*)*)"

if (line textstr) {
  string sResult = textstr[match 1] 
  // Now, trim off the "section(s) " part, if necessary
  sResult = (sResult[7]=='s') ? sResult[9:] : sResult[8:]
  print sResult"\n"
}

 

Good luck.

 

Re: regular exp help needed
llandale - Thu Jan 09 15:39:06 EST 2014

What are you trying to do?  It appears you want to turn the first line into the 2nd:

  • ABC, sections <anything>
  • <anything>

Allowing for "section" as well.

First off, the following is a great resource: http://en.wikipedia.org/wiki/Regexp, where for you we find that the "?" can match zero or one time, which you want with your "s".

  • string textstr = "ABC, sections 3.3.3 xyz"
  • string sLine = "(ABC, sections? )(.*)"
  • print "[" sLine "]\n"
  • Regexp line = regexp2(sLine)
  •  
  • if (line textstr)
  • {  print "match 0: [" textstr[match 0] "]\n"
  •    print "match 1: [" textstr[match 1] "]\n"
  •    print "match 2: [" textstr[match 2] "]\n"  // << I think this is the result you want
  •    print "match 3: [" textstr[match 3] "]\n"  // no such match
  • }

We notice that if there is a match, [match 0] is always the entire string, [match 1] is the first parenthesis group encountered, [match 2] is the 2nd, etc.

Your result, it seems, then is textstr[match 2].

I really struggle with Regular expressions and so always stage the string in a string variable, so I can print it which drastically helps me debug it.

-Louie

Now perhaps what you REALLY want is to notice that this is for ABC, then recursively parse out each and every numbered section, skipping such words as "through" and "and".  If so I'd be tempted to take the value between spaces, verify it appears to be a well formed section (it is all digits and periods), and use that.

Re: regular exp help needed
EHcnck - Thu Jan 09 16:32:50 EST 2014

llandale - Thu Jan 09 15:39:06 EST 2014

What are you trying to do?  It appears you want to turn the first line into the 2nd:

  • ABC, sections <anything>
  • <anything>

Allowing for "section" as well.

First off, the following is a great resource: http://en.wikipedia.org/wiki/Regexp, where for you we find that the "?" can match zero or one time, which you want with your "s".

  • string textstr = "ABC, sections 3.3.3 xyz"
  • string sLine = "(ABC, sections? )(.*)"
  • print "[" sLine "]\n"
  • Regexp line = regexp2(sLine)
  •  
  • if (line textstr)
  • {  print "match 0: [" textstr[match 0] "]\n"
  •    print "match 1: [" textstr[match 1] "]\n"
  •    print "match 2: [" textstr[match 2] "]\n"  // << I think this is the result you want
  •    print "match 3: [" textstr[match 3] "]\n"  // no such match
  • }

We notice that if there is a match, [match 0] is always the entire string, [match 1] is the first parenthesis group encountered, [match 2] is the 2nd, etc.

Your result, it seems, then is textstr[match 2].

I really struggle with Regular expressions and so always stage the string in a string variable, so I can print it which drastically helps me debug it.

-Louie

Now perhaps what you REALLY want is to notice that this is for ABC, then recursively parse out each and every numbered section, skipping such words as "through" and "and".  If so I'd be tempted to take the value between spaces, verify it appears to be a well formed section (it is all digits and periods), and use that.

Regexp line = regexp2 "[A-Za-z0-9_]+, *sections* *((([0-9]+\\.)+[0-9]+,* *)+(and *([0-9]+\\.)+[0-9]+ *)*)"

 

Thank all for the help